package com.tang.transmission.interceptor;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
@Order(1)
public class GatewayFromInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果不是网关来的服务，都拒绝访问
        String fromKey = request.getHeader("systemFrom");
        if(fromKey == null || !"gateway".equals(fromKey)){
            response.setContentType("application/json; charset=utf-8");
            PrintWriter writer = response.getWriter();
            //权限不足
            response.setStatus(401);
            writer.write("error");
            return false;
        }
        return true;
    }
}